package com.service.imp;

import com.common.Base;
import com.common.Result;
import com.entity.FollowUser;
import com.repository.FollowUserRepository;
import com.service.FollowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @Author：Luohuiyong
 * @Description:
 * @Date: Created in 11:06 2019/1/9
 */
@Service
public class FollowUserServiceImp implements FollowUserService {

    @Autowired
    private FollowUserRepository followUserRepository;

    @Override
    public FollowUser findOne(int id){
        Optional<FollowUser> optionalTopic = followUserRepository.findById(id);
        return optionalTopic.get();
    }

    @Transactional
    @Override
    public Result create(FollowUser bean) {
        if(bean.getFollowedUser() == bean.getFollowUser()){
            return Base.error("你是要关注你自己吗？");
        }
        List<FollowUser> resList = followUserRepository.findAllByFollowUser(bean.getFollowUser());
        for (int i = 0; i < resList.size();i++){
            if(bean.getFollowedUser() == resList.get(i).getFollowedUser()){
                return Base.error("已关注过该作者");
            }
        }
        followUserRepository.save(bean);
        return Base.success();
    }
}
